import Vue from 'vue'
import VueRouter from 'vue-router'

import store from '../store';
import { start, done } from '@/utils/nprogress';

Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}





const routes =
  [
    {
      // 菜单列表里不应该显示app路由
      path: "/app",
      name: "app",
      component: () => import('../layout.vue'),
      meta: { title: "app", keepAlive: true },
      children: [
        {
          path: "/home",
          name: "home",
          component: () => import('@/views/home'),
          meta: { title: "首页", icon: "el-icon-s-home" },
        },
        {
          path: "/menu1",
          name: "Menu1",
          meta: { title: "Menu 1", icon: "el-icon-s-promotion", keepAlive: true },
          component: () => import('@/views/nested'),
          children: [
            {
              path: "/menu1-1",
              component: () => import('@/views/menu1-1'),
              name: "Menu1-1",
              meta: { title: "Menu 1-1" },
            },
            {
              path: "/menu1-2",
              component: () => import('@/views/nested'),
              name: "Menu1-2",
              meta: { title: "Menu 1-2", keepAlive: true },
              children: [
                {
                  path: "/menu1-2-1",
                  component: () => import('@/views/menu1-2-1'),
                  name: "Menu1-2-1",
                  meta: { title: "Menu 1-2-1", disabled: true },
                },
                {
                  path: "/menu1-2-2",
                  component: () => import('@/views/menu1-2-2'),
                  name: "Menu1-2-2",
                  meta: { title: "Menu 1-2-2", keepAlive: true, permissions: ["admin","button"], },

                },
              ],
            },
            {
              path: "/menu1-3",
              component: () => import('@/views/menu1-3'),
              name: "Menu1-3",
              meta: { title: "Menu 1-3" },
            },
          ],
        },
        {
          path: "/setting",
          name: "setting",
          component: () => import('@/views/nested'),
          meta: { title: "系统设置", icon: "el-icon-setting",permissions:["admin"] },
          children:[
            {
              path: "/user",
              component: () => import('@/views/user/user/index.vue'),
              name: "user",
              meta: { title: "用户管理"},
            },
            {
              path: "/role",
              component: () => import('@/views/user/role/index.vue'),
              name: "role",
              meta: { title: "角色管理"},
            },
            {
              path: "/permission",
              component: () => import('@/views/user/permission/index.vue'),
              name: "permission",
              meta: { title: "权限管理"},
            },
            {
              path: "/operationLog",
              component: () => import('@/views/operationLog/index.vue'),
              name: "operationLog",
              meta: { title: "操作日志"},
            },
            {
              path: "/fileInfo",
              component: () => import('@/views/FileInfo/index.vue'),
              name: "fileInfo",
              meta: { title: "文件管理"},
            },
            {
              path: "/quartz",
              component: () => import('@/views/quartz/index.vue'),
              name: "quartz",
              meta: { title: "定时任务"},
            },
          ]
        }
      ]
    },
    {
      path: "/login",
      name: "login",
      component: () => import('@/views/login'),
      meta: { title: "登录"},
    }


  ]


const router = new VueRouter({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  // start()
  // 避免重复导航
  if (to == next) {

  }
  else {
    let too = { fullPath: to.fullPath, name: to.name, title: to.meta.title }
    if (!store.getters.tagView.some(item => item.name == to.name)) {

      if (to.name != "home" && to.name != "login" && to.name !=null) {
        store.commit('addTagView', too)
      } else {
        store.commit('resetActiveTagView')
      }
    } else {
        store.commit('fromNavRefreshTagView', too)
    }
    next()

  }
})
router.afterEach((to, from) => {
  var bread = []
  
  to.matched.forEach((item, index) => {
    if (index > 0) {
      bread.push({ name: item.name, title: item.meta.title })
    }
  }) 
    store.dispatch('setBreadCrumb', bread)
  // done()
})

export default router